package top.jacktgq.lock.reentrantLock;

import top.jacktgq.utils.LogUtils;
import top.jacktgq.utils.Sleeper;
/**
 * 
 * @Title: TestReentrantLock.java 
 * @Package top.jacktgq.lock 
 * @Description: 测试ReentrantLock：条件变量
 * @author CandyWall   
 * @date 2020年11月1日 下午5:49:07 
 * @version V1.0
 */
public class Demo4_1_wait_notify {
	private static Object room = new Object();
	private static boolean hasCigarette = false;	//有没有烟
	private static boolean hasTakeout = false;		//有没有外卖
	public static void main(String[] args) {
		new Thread(() -> {
			synchronized (room) {
				LogUtils.log("Demo4_1", "有烟没？" + hasCigarette);
				while(!hasCigarette) {
					LogUtils.log("Demo4_1", "没烟，先歇会儿！");
					try {
						room.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
				LogUtils.log("Demo4_1", "有烟没？" + hasCigarette);
				LogUtils.log("Demo4_1", hasCigarette ? "可以开始干活了！" : "没干成活...");
			}
		}, "小南").start();
		new Thread(() -> {
			synchronized (room) {
				LogUtils.log("Demo4_1", "外卖送到没？" + hasTakeout);
				while(!hasTakeout) {
					LogUtils.log("Demo4_1", "没送到，先歇会儿！");
					try {
						room.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
				LogUtils.log("ReentrantLockDemo4_1", "外卖送到没？" + hasTakeout);
				LogUtils.log("ReentrantLockDemo4_1", hasTakeout ? "可以开始干活了！" : "没干成活...");
			}
		}, "小女").start();
		
		Sleeper.sleep(1);
		new Thread(() -> {
			synchronized (room) {
				hasTakeout = true;
				LogUtils.log("Demo4_1", "外卖送到了哟！");
				room.notifyAll();
			}
		}, "外卖小哥").start();
		new Thread(() -> {
			synchronized (room) {
				hasCigarette = true;
				LogUtils.log("Demo4_1", "烟买来了！");
				room.notifyAll();
			}
		}, "小弟").start();
	}	
}
